<%@page import="BusinessLogic.CoursesContainer"%>
<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="BusinessLogic.User" %>
<%@ page import="BusinessLogic.*" %>
<%@ page import="BusinessLogic.Session" %>
<link href="public/style.css" rel="stylesheet" type="text/css" media="screen">
<title>Easy Schedule</title>
<script type="text/javascript" src="public/jquery.js"></script>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
Result res;
Map<String, String[]> params = new HashMap<String,String[]>();
params.clear();
params.put("action", new String[]{"getTimetable"});
params.put("USER", new String[]{request.getUserPrincipal().getName()});
res = requestHandler.handle(params);
String xsl=application.getInitParameter("XSL");
if (request.getParameter("xsl")!=null)
	xsl = request.getParameter("xsl");
session.setAttribute("timetable", ((CoursesContainer)res.getSuccessReturnValue()).getXML());%>
<script type="text/javascript"> 
function selectionChanged()
{
	if($("#disp option:selected").val()=='own')
		{
			$('#myxsl').removeClass('adminkey');
		}
	else
		{
			window.location.href = "./timetable?xsl="+$("#disp option:selected").val();
		}
	
}
function goxsl()
{
	$.get(
		    "./URLgetter",
		    {xsl:$('#xsl').val()},
		    function(data) { 
		    	var parser=new DOMParser();
		        var xml=parser.parseFromString(<%=session.getAttribute("timetable")%>,'text/xml');
		    	xsltProcessor=new XSLTProcessor(); 
		     xsltProcessor.importStylesheet(data); 
		     resultDocument = xsltProcessor.transformToFragment(xml,document); 
		     $('#tt').html(resultDocument);
			 },
		    "xml"
		);
	
}
function loadXMLDoc(fname) { 
  var xmlDoc; 
 
 // code for IE 
  if (window.ActiveXObject) { 
      xmlDoc=new  ActiveXObject("Microsoft.XMLDOM"); 
  } 

  // code for Mozilla, Firefox, Opera, etc. 
  else if (document.implementation && document.implementation.createDocument) { 
      xmlDoc=document.implementation.createDocument("","",null); 
  } else { 
       alert('Your browser cannot handle this script'); 
  } 
  xmlDoc.async=false; 
  xmlDoc.load(fname); 
  return(xmlDoc); 
}

function displayResult(xmlString) { 
	<%if (request.getParameter("xsl")!=null) {%>
	$("select#disp option[selected]").removeAttr("selected");
	$("select#disp option[value='<%=request.getParameter("xsl")%>']").attr("selected", "selected");
	<%}%>
	var parser=new DOMParser();
    var xml=parser.parseFromString(<%=session.getAttribute("timetable")%>,'text/xml');
    xsl=loadXMLDoc("<%=xsl%>"); 
    // code for IE 
    if (window.ActiveXObject) { 
        ex=xml.transformNode(xsl);     
        document.getElementById("tt").innerHTML=ex; 
    } 
    // code for Mozilla, Firefox, Opera, etc. 
   else if (document.implementation && document.implementation.createDocument) 
   { 
         xsltProcessor=new XSLTProcessor(); 
         xsltProcessor.importStylesheet(xsl); 
         resultDocument = xsltProcessor.transformToFragment(xml,document); 
         $('#tt').html(resultDocument);
    } 
} 

</script> 

</head>

<body onload = "displayResult();">
<div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
	        <div id="main">	
	            <div id="sidebar">
	                <div class="box" id="sidebar_head">
	                      <h2><a id="loginAnchor">User</a></h2>
	                    <div class="box2">
                        <ul>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/search">Search courses</a></li>
                            <li><a href="/tamirandevyatar/timetable">My timetable</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                            <li><a href="/tamirandevyatar/users">View users</a></li>
                        </ul>
                        </div>
	                </div>		
	            </div><!-- sidebar -->    	              
	            <div id="content">
	                <div id="contheader">
	                    <h2>View your semester schedule</h2>
	                </div>		
	                <div id="content2" class="welcome">
	                <label for="disp">Display as&nbsp;&nbsp;</label>
					    	<select name="disp"  id="disp" onchange="selectionChanged()">
								<option value="public/xsl1.xsl">Table</option>
								<option value="public/xsl2.xsl">List</option>
								<option value="own">Your own!</option>
							</select>
							<div id="myxsl" class="adminkey">
							<label for="xsl">Enter your own xsl url&nbsp;&nbsp;</label>
							<input type="text" name="xsl" value="" id="xsl"/>
							<button type="button" onclick="goxsl()">Go!</button>
							</div>
	                	<div id ="tt" class="entry">
						
					</div>
                </div><!-- content2 -->								              
            </div><!-- content -->                    
            <div class="clearing">&nbsp;</div> 
            <div id="footer">
                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
            </div>
        </div><!-- main -->
    </div><!-- page -->
</body>
</html>